﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forwarder.Web.Models;
using Forwarder.Data;

namespace Forwarder.Web.Controllers
{
    public class HomeController : Controller
    {

        //主页
        public ActionResult Index()
        {
            return View();
        }



        #region 注册与登录


        //企业用户注册
        [HttpGet]
        public ActionResult Logon()
        {
            LogonModel model = new LogonModel()
            {
                UserData = null,
                PageTitle = "企业注册",
                Message = "",
            };
            return View(model);
        }


        [HttpPost]
        [ActionName("Logon")]
        public ActionResult Logon_post()
        {
            var unitname = HttpContext.Request.Form["unitname"];
            var unitcode = HttpContext.Request.Form["unitcode"];
            var password = HttpContext.Request.Form["password"];

            LogonModel model = new LogonModel()
            {
                UserData = null,
                PageTitle = "企业注册",
                Message = "",
            };

            //值检测
            if (string.IsNullOrWhiteSpace(unitname))
            {
                model.Message = "企业名称不能为空";
            }
            else if (string.IsNullOrWhiteSpace(unitcode))
            {
                model.Message = "企业代号不能为空";
            }
            else if (string.IsNullOrWhiteSpace(password))
            {
                model.Message = "管理员密码不能为空";
            }
            else
            {
                //重复检测  unitecode
                //注册并转到消息页面提示试用admin@企业代号 进行登录
            }

            return View(model);
        }


        //用户登录
        [HttpGet]
        public ActionResult Login()
        {
            var model = new LoginModel()
            {
                  PageTitle = "用户登录",
            };

            return View(model);
        }


        [HttpPost]
        [ActionName("Login")]
        public ActionResult Login_post()
        {
            //获取
            var loginname = HttpContext.Request.Form["loginname"];
            var password = HttpContext.Request.Form["password"];

            //登录名格式检查
            var logins = loginname.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
            var username = logins[0];
            var unitcode = logins[1];

            //验证用户
            var msg = DBAPI.VerifyUser(unitcode, username, password);

            if (msg == "成功")
            {
                //获取用户对象

                var roles = "";
                //
                var authcookie = Spore.Tools.GetWebAuthCookie(username, roles, DateTime.Now.AddDays(-1));

                Response.SetCookie(authcookie);


                if (username == "admin")
                {
                    return RedirectToAction("main", "home");
                }
                else
                {
                    return RedirectToAction("main", "admin");
                }

            }

            var model = new LoginModel()
            {
                Loginname = loginname,
                Message = msg,
            };

            return View(model);

        }


        #endregion


        #region 业务页面

        //列表页面
        [Authorize]
        public ActionResult Main()
        {
            return View();
        }


        [Authorize]
        public ActionResult order_list_json()
        {
            return View();
        }


        //订单表单窗口页面
        [Authorize]
        public ActionResult OrderForm()
        {
            return View();
        }


        [Authorize]
        public ActionResult order_detail_json()
        {
            return View();
        }



        //字典数据json
        [Authorize]
        public ActionResult dict_json()
        {
            return View();
        }


        #endregion



    }
}
